Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Here document syntax, what is $0 (was: Newbie need a clue)

by nysus (Parson)
on Mar 29, 2001 at 06:44 UTC ( [id://68029]=perlquestion: print w/replies, xml ) Need Help??

nysus has asked for the wisdom of the Perl Monks concerning the following question:

OK, I bow humble before thee, honorablest Perl monks:

What is this 'EOUsage' in the code below? And what is that funky '$0' variable? I obviously didn't find it too helpful, but here's what the perldoc says about $0:

Contains the name of the file containing the Perl script being executed. On some operating systems assigning to ``$0'' modifies the argument area that the ps(1) program sees. This is more useful as a way of indicating the current program state than it is for hiding the program you're running. (Mnemonic: same as sh and ksh.)

I'm guessing, from the way it looks, it automatically comments out the lines but how exactly does it work? Here's the subroutine code I have questions on, edited for brevity:

sub usage { $0 =~ s#.*/##; print STDERR <<EOUsage; Usage: $0 [options] where options are: [-v ] | [--version ] [more of the same...deleted...deleted...deleted...deleted] [-db <n> ] | [--debug <n> ] [-pm/+pm ] | [--preformat-marker / --nopreformat-marker ] More complete explanations of these options can be found in comments near the beginning of the script. EOUsage # [-T <t>:<r> ] | [--tag <tagname>:<regexp> ] }

Any other insights into what this subroutine is doing is much appreciated. Thanks!

2001-03-29 Edit by Corion : Changed title

Replies are listed 'Best First'.
Re: Newbie need a clue
by kschwab (Vicar) on Mar 29, 2001 at 07:02 UTC
    The "EOUsage" is the terminator for what's referred to as a here document. Note the print STDERR <<EOUsage; line several lines above it. Basically, everything between that line and the terminator gets printed to STDERR. Try perldoc perldata for more info, search for the words "here document".

    The quoted text seems lucid to me. Perhaps I can rephrase a little ? If this script exists on a unix box as /usr/local/bin/myscript, $0 could contain one of several values, depending on how it was called:

    How script was startedContents of $0
    $ myscriptmyscript
    $ /opt/local/bin/myscript/opt/local/bin/myscript
    $ cd /opt/local/bin;./myscript./myscript

    The following bit: $0 =~ s#.*/##; is a regex that strips everything from the front of $0 until it reaches the last "/" character. So, if the script is named myscript, after this regex is run against $0, $0 would contain myscript, regardless of how it was called.

Re: Newbie need a clue
by damian1301 (Curate) on Mar 29, 2001 at 06:52 UTC
    1. The EOUSAGE is just how the program is supposed to be used, inside of a here document. With here documents, you don't have to escape characters or anything and they can interpolate variables as well. You should read up on them.

    2. The $0 variable is just what you said, it is the path to where the script is executing. What happens is with that substitution, it gets everything up to a slash in $0 (the .* part does that which should really be $0 =~ m#.*\\(.*)#; or so...) and deletes it.

    Almost a Perl hacker.
    Dave AKA damian

    I encourage you to email me

      Aha! But beware! Dot star will cause you trouble for Many Moons. See why here.

      redmist
      Silicon Cowboy
        Thank you and I am glad you pointed that out. I tried to get all the greediness I could on that because I wanted the expression to get all of the characters up to the last slash, and then the script's name that would follow. So, greediness can be good at times.

        Almost a Perl hacker.
        Dave AKA damian

        I encourage you to email me
Re: Newbie need a clue
by physi (Friar) on Mar 29, 2001 at 12:23 UTC
    Well just a hint:
    use File::Basename
    $0=basename $0;<br>
    this will do the same job as $0 =~ s#.*/##;
    But maybe it`s better to understand.
    ----------------------------------- --the good, the bad and the physi-- -----------------------------------
Re: Here document syntax, what is $0 (was: Newbie need a clue)
by merlyn (Sage) on Mar 29, 2001 at 19:48 UTC
    Beware... that regex is incomplete. You'll want:
    $0 =~ s#.*/##s;
    Or else you'll end up stopping on any newline in the string! Yes, unix paths can contain newlines in them.

    -- Randal L. Schwartz, Perl hacker

Re: Newbie need a clue
by nysus (Parson) on Mar 29, 2001 at 07:26 UTC
    Thanks, dudes. This helps. I'm still a little baffled by exactly how that regexp works. It looks like it would replace every instance of 'any character' in the script name with a '#'. What is making it stop at the last slash?
      the # in this case is the delimiter (since it's searching for a /). first thing after the "s" is the delimiter; often a /, but not always.
        Ah! Of course! Now I see it. Forgive me, I've only been at this stuff a few weeks.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://68029]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-20 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found