Solved

Perl syntax

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

Thanks,


0
Comment
Question by:Tolgar
5 Comments
 
LVL 8

Assisted Solution

by:point_pleasant
point_pleasant earned 80 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 26

Assisted Solution

by:wilcoxon
wilcoxon earned 80 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 90 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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This video discusses moving either the default database or any database to a new volume.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now