Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Taking Keyboard Input from the command line

by OxYgEn (Acolyte)
on Feb 26, 2004 at 00:57 UTC ( #331882=perlquestion: print w/replies, xml ) Need Help??
OxYgEn has asked for the wisdom of the Perl Monks concerning the following question:

I've seen it done in some scripts I've downloaded, but after carefully searching them, I don't see how they've done it. Basically all I'm trying to do is add or remove stuff from an array, so I could run something like this:

perl add something

and "something" would be added to a predefined array. Alternatively, to remove "something" from an array, I would type

perl del something

Thanks for your patience on my incredibly stupid questions.
  • Comment on Taking Keyboard Input from the command line

Replies are listed 'Best First'.
Re: Taking Keyboard Input from the command line
by Abigail-II (Bishop) on Feb 26, 2004 at 01:03 UTC
    Something like:
    $ARGV [0] eq "add" ? do {push @predefined => $ARGV [1]} : $ARGV [0] eq "del" ? do {@predefined = grep {$_ ne $ARGV [1]} @predefi +ned} : die "Unknown command"


Re: Taking Keyboard Input from the command line
by Limbic~Region (Chancellor) on Feb 26, 2004 at 01:04 UTC
    I am not exactly sure this is what you want, but this should get you started. You might also want to take a look at Getopt::Std
    #!/usr/bin/perl use strict; use warnings; my @array = qw(foo bar blah asdf); if ( defined $ARGV[0] && defined $ARGV[1] ) { push @array , $ARGV[1] if $ARGV[0] eq 'add'; @array = grep {$_ !~ /^$ARGV[1]$/} @array if $ARGV[0] eq 'del'; } print "$_\n" for @array;
    Cheers - L~R

    Update: Corrected syntax error

Re: Taking Keyboard Input from the command line
by OxYgEn (Acolyte) on Feb 26, 2004 at 23:55 UTC
    Limbic~Region, I believe that is what I am looking for, but when I use your code this is what I get:

    Array found where operator expected at line 8, near "/^$ARGV1$/ "
    (Missing operator before ?)
    syntax error at line 8, near "/^$ARGV1$/ @array "
    Execution of aborted due to compilation errors.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://331882]
Approved by kvale
[erix]: I might if I did remember. It's probably somewhere in the site docs
[uhClem]: I don't favor that sort of thing around here but the place for our politics is wherever we live.
[uhClem]: And besides, the objection to it was off-topic.
[Discipulus]: 1nickt hide signature is a positive action; i think is not a solution here
[erix]: my objection to sigs is just that they take screen space. ( Some monks would have *several* slogans underneath a 1-liner reply )

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (12)
As of 2017-06-22 12:39 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (519 votes). Check out past polls.