Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

optional command line arguments

by Molten (Initiate)
on Aug 07, 2012 at 18:19 UTC ( #986051=perlquestion: print w/ replies, xml ) Need Help??
Molten has asked for the wisdom of the Perl Monks concerning the following question:

This is my attempt to emulate a block in C++. (I'm not positive of the official name, but here is the syntax)
int b = ({ multiple commands here; final command = return value; });
I would like to use something similar in perl. (this is what I want, but it is incorrect)
my $a = shift || ({ print "enter in value for a:"; <STDIN>; });
but I couldn't find anything similar. these are my two attempts to emulate it, but neither is perfect. For the first one ($a), you have to create a separate function for each distinct block you want to create. The second one just looks like horrible coding.
#!/user/local/bin/perl use warnings; use strict; sub IN($){ print shift; my $ret = <STDIN>; chomp $ret; return $ret; } my $a; my $b; $a = shift || IN("Enter value for b:"); if($b = shift){} else{ print "Enter value for c:"; chomp ($b = <STDIN> +); } print "a: $a\n"; print "b: $b\n";
So my question to you is, what is the cleanest way you take in command line arguments, and if they don't exist, prompt the user for the values?     Oh, and also, is there a block type syntax similar to the C++ one I put at the top?

Comment on optional command line arguments
Select or Download Code
Re: optional command line arguments
by davies (Vicar) on Aug 07, 2012 at 18:36 UTC
Re: optional command line arguments
by ig (Vicar) on Aug 07, 2012 at 19:42 UTC
    is there a block type syntax similar to the C++ one I put at the top?

    You can use do

    my $b = do { multiple_commands_here; final_command; };
Re: optional command line arguments
by influx (Beadle) on Aug 07, 2012 at 20:39 UTC

    How about using prototypes to emulate your own block?

    sub block(&) { shift->(); } my $a = shift @ARGV || block { print "Enter value for a:"; <STDIN> }; print "$a\n";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-09-30 20:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (383 votes), past polls