Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Function Prototype Context Conversion

by tadman (Prior)
on Jun 13, 2002 at 13:02 UTC ( [id://174178]=note: print w/replies, xml ) Need Help??


in reply to Re: Function Prototype Context Conversion
in thread Function Prototype Context Conversion

Well, it is clear the way Perl operates, but that is merely one interpretation of how it could be done. What burned me was that I do this sort of thing all the time with regular, unprototyped functions. It just goes to show you never can be too careful when programming.

I suppose the unfortunate thing is that Perl doesn't differentiate between "scalars" and scalar-like things in the same way that it differentiates between "arrays" and lists. Using prototypes to merely mandate how many paramters should be passed to a function doesn't seem to be the best way to go about it anyway.

Maybe I'm too shell oriented, where I expect things to be automatically expanded before being processed. Meaning, something like this:
% /bin/foo foo_a foo_b foo_c
Where there you have three distinct parameters being passed to /bin/foo and it will complain if it doesn't have, let's say, at least two.
% /bin/foo foo_?
Now here there's technically one element being passed to the shell, which expands it into a list before being proccesed. Same thing goes if you had a variable $FOO which was 'foo_a foo_b foo_c' of course.

In summary, I suppose the only way to do this reliably is something like this:
sub foo { croak "foo() only takes one argument" if (@_ > 1); print "$_[0]\n"; }
But you're not likely to see that put into general practice any time soon.

I understand that Perl and the shell are two different things, so this whole thing is really just one big assumption that I made.

The only reason I would even think of Perl functions and command-line programs as the same thing is the whole laissez-faire way of handling arguments that Perl has. They're not typed (normally), they're not named, heck, they're not even declared unless you use prototypes! In that regard, I was figuring that @_ would operate along the same lines as @ARGV since the two are so alike in that respect.

Replies are listed 'Best First'.
Re: Function Prototype Context Conversion
by Abigail-II (Bishop) on Jun 13, 2002 at 13:14 UTC
    It all has to do with compile time and run time. Prototyping has a compile time effect, while the number of elements in an array isn't known till run time. I fail the see the relevancy of your shell example. Prototyping changes the way perl parses Perl. Expecting no changes when using prototypes is a mistake (if you don't want changes, don't change your code! ;-)).

    Abigail

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-04-19 15:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found