Click to See Complete Forum and Search --> : perl snippet(s)


YaRness
12-14-2000, 11:48 AM
there really hasn't been enough perl on here in the last week or day or hour, so i thought i'd post something.

here's a simple bit of code to take a particular parameter (i.e. matching some pattern) from the command line, or if there isn't one, it asks for the input from the user, or quits if the user enters "q" or "Q".


$_=$ARGV[0] or $_="";

while (! /<some pattern>/)
{
print "Enter input: ";
$_=<STDIN>;
exit if (/^[qQ]$/);
}



i ended up using a substition in the while control, so it looked something like this


$_=$ARGV[0] or $_="";

while (! s/^([a-zA-Z]+)([0-9]+).*$/$1 $2/)
{
print "Enter input: ";
$_=<STDIN>;
exit if (/^[qQ]$/);
}

tr/a-z/A-Z/;
my ($prefix, $suffix) = split();


so if i entered "aBc123" either on the command line or as requested by the program, $prefix would contain "ABC" and $suffix would contain "123".

<edit> (actually dont need a pattern just for matching q, but i actually have /^[qQ].*$/ since the values i'm looking for will never start with q http://www.linuxnewbie.org/ubb/biggrin.gif )
------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
------------------

[This message has been edited by YaRness (edited 14 December 2000).]

TheLinuxDuck
12-14-2000, 12:06 PM
Shoot, I haven't hardly touched perl over the last week. I dunno why. Girl on the brain, I guess. http://www.linuxnewbie.org/ubb/smile.gif

------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq

YaRness
12-14-2000, 12:16 PM
BAD DUCK. VERY BAD DUCK. perl is funner than girls. wait i didn't just say that.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
------------------

TheLinuxDuck
12-14-2000, 01:15 PM
Originally posted by YaRness:
BAD DUCK. VERY BAD DUCK. perl is funner than girls. wait i didn't just say that.


Normally, I would agree with you, except for this very special woman that has become a part of me... but, it doesn't mean I'm done programming!!! http://www.linuxnewbie.org/ubb/smile.gif http://www.linuxnewbie.org/ubb/smile.gif

I just need a project to work on.

------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq

YaRness
12-14-2000, 01:45 PM
i've been helping someone do some web stuff, i found a good module, HTTP::Request::Form, that makes it really easy to fill out and submit a web site form. i downloaded the tarball for it and just adapted the altavista.pl script that comes with it. now i just have to learn how all the dependent modules work (not to mention my sorta constant effort to get better at OOP and do it in perl too).

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
------------------

jemfinch
12-14-2000, 03:59 PM
Shouldn't you be chomping that "$_=<STDIN>;"?

Jeremy

YaRness
12-14-2000, 04:15 PM
i dunno, is there a reason to?

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
------------------

TheLinuxDuck
12-14-2000, 06:04 PM
Originally posted by YaRness:
i dunno, is there a reason to?


There's always a reason to chomp sdtin!! http://www.linuxnewbie.org/ubb/smile.gif

Actually, the regexp pretty much keels the newline, so you don't in this case (which you prolly already knew.. http://www.linuxnewbie.org/ubb/smile.gif)

------------------
TheLinuxDuck
Wait... that's a penguin?!?!?
:wq

nanode
12-14-2000, 07:23 PM
Don't make me paste a java snippet... http://www.linuxnewbie.org/ubb/smile.gif

Perl is cool and I need to get back into it. I put the 3rd Ed. Camel book on my Christmas List this year, so I'll be a badass by early spring for sure http://www.linuxnewbie.org/ubb/tongue.gif

jemfinch
12-14-2000, 08:23 PM
Originally posted by YaRness:
i dunno, is there a reason to?


In the future, you may be using s or m modifiers on your regexp that will affect whether $ matches when there's a newline at the end of a string. In those cases (though not in this particular one) chomp will be required.

Jeremy

YaRness
12-14-2000, 10:49 PM
TLD and jemfinch pretty much nailed it. since i'm extracting what i need from the matched text and not really using the whole matched entity for anything (and in this case never will), then i really don't need to chomp it. i could prolly do without the "^" and "$" (well, at least the "$"), but it makes me happier to keep patterns as explicit as possible.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/
------------------