Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Perl syntax

Posted on 2011-03-24
5
Medium Priority
?
384 Views
Last Modified: 2012-05-11
What does $@ and $? mean in Perl?

Thanks,


0
Comment
Question by:Tolgar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 8

Assisted Solution

by:point_pleasant
point_pleasant earned 320 total points
ID: 35209564
$@
The Perl syntax error message from the last eval() operator. If $@ is the null string, the last eval() parsed and executed correctly (although the operations you invoked may have failed in the normal fashion). (Mnemonic: Where was the syntax error "at"?)

$? may be set to non-0 value if an external program  fails. The upper eight bits reflect specific error conditions encountered by the program (the program's exit() value). The lower eight bits reflect mode of failure, like signal death and core dump information
0
 

Author Comment

by:Tolgar
ID: 35209944
ok. One additional question in the same same context:

sub SendMail {
    my ($subj, $msg, @users) = @_;

SOME CODE
}

What does this line mean? Especially with @_  


Thanks,
0
 
LVL 27

Assisted Solution

by:wilcoxon
wilcoxon earned 320 total points
ID: 35210040
@_ inside a subroutine is all of the arguments passed to that subroutine.  It means that the values passed to SendMail will be assigned in order to the vars $subj, $msg, and @users.

If you call SendMail('test msg', 'this is a test', 'jim', 'bob', 'ron') then
$subj = 'test msg'
$msg = 'this is a test'
@users = ('jim', 'bob', 'ron')
0
 
LVL 8

Expert Comment

by:point_pleasant
ID: 35210075
@_ is the list of incoming parameters to a sub. So if you write a sub, you refer to the first parameter in it as $_[0], the second parameter as $_[1] and so on. And you can refer to $#_ as the index number of the last parameter:
0
 
LVL 48

Accepted Solution

by:
Tintin earned 360 total points
ID: 35210642
If you type in

perldoc perlvar

you'll see:

      $?      The status returned by the last pipe close, backtick ("``") command, successful call to wait() or waitpid(), or
               from the system() operator.  This is just the 16-bit status word returned by the traditional Unix wait() system
               call (or else is made up to look like it).  Thus, the exit value of the subprocess is really ("$? >> 8"), and
               "$? & 127" gives which signal, if any, the process died from, and "$? & 128" reports whether there was a core
               dump.  (Mnemonic: similar to sh and ksh.)

               Additionally, if the "h_errno" variable is supported in C, its value is returned via $? if any "gethost*()"
               function fails.

               If you have installed a signal handler for "SIGCHLD", the value of $? will usually be wrong outside that
               handler.

               Inside an "END" subroutine $? contains the value that is going to be given to "exit()".  You can modify $? in
               an "END" subroutine to change the exit status of your program.  For example:

                   END {
                       $? = 1 if $? == 255;  # die would make it 255
                   }

               Under VMS, the pragma "use vmsish 'status'" makes $? reflect the actual VMS exit status, instead of the default
               emulation of POSIX status; see "$?" in perlvms for details.

               Also see "Error Indicators".

       $@      The Perl syntax error message from the last eval() operator.  If $@ is the null string, the last eval() parsed
               and executed correctly (although the operations you invoked may have failed in the normal fashion).  (Mnemonic:
               Where was the syntax error "at"?)

               Warning messages are not collected in this variable.  You can, however, set up a routine to process warnings by
               setting $SIG{__WARN__} as described below.

               Also see "Error Indicators".


       @_      Within a subroutine the array @_ contains the parameters passed to that subroutine.  See perlsub.



0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question