Really, I do not recommend explicitly defining the @_ var, but I hope it is just for example purposes.

So in that case....

#! /usr/bin/perl -w use strict; @_ = qw( Lucy, I'm home! ); sub main { while(my $d = shift) { print "$d\n"; } } &main;

You can use this if it really upsets you.

However, assuming your code is not just an example, why in the world are you using the @_ array??? And if your not explicitly defining something to @_, there should not be a variable to read in the main. The package main should always be called first on program execution, and should have @ARGV, and the variables you assigned (globally...). (Side note, yes, I know other built-ins are there, but these are really the vars he needs to deal with.)

Dont just think of @_, $_, etc, as "default variables", but more as pronouns. "These are as bad as it." Is not a proper sentence, and a worse program. "These are apples, and it is an orange. These cannot compare to it." Now the sentence makes no sense for a reason other than bad grammar.

So from what I can tell, you're doing something you shouldn't, and perl is making you type three more charachters... Whatta language.

What would happen to this:

#!/usr/bin/perl -w use strict; @_ = qw( hello world ) @other = qw( hasta la vista baby ) sub main { while(my $d = shift) { print "$d\n"; # Are we coming or going? } } &main;

The code actually reads the @_ array, which it should, but more importantly, you can't tell if you're coming or going just from reading it.

Bad monky...

~Hammy


In reply to Re: @_ the default variable? by HamNRye
in thread @_ the default variable? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.