Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Backticks and Pattern Matching.

by misconfiguration (Sexton)
on Feb 05, 2008 at 15:29 UTC ( [id://666315]=perlquestion: print w/replies, xml ) Need Help??

misconfiguration has asked for the wisdom of the Perl Monks concerning the following question:

I've come to seek more of the infinite wisdom! I have a chunk of code setup, if invoked it goes through the process to add a printer queue to Unix. I have a function built in to check the status of the print spooler.

if ($add) { $add = lc($add); if ( -e "/var/spool/lp/request/$add" ) { die "This printer is already defined!\n"; } else { my %add_cmd = ( add => "addqueue -h $add -q $add -i 3" ); # Lab Version will be more sophisticated print "Adding queue \"$add\"\n"; system( $add_cmd {'add'} ); sleep 5; # If spooler is down, automatically restart it. print "Checking the status of the print spooler....\n"; my $spooler = `lpstat -r`; if ( $spooler = "scheduler is not running" ) { system("lpsched"); } sleep 5; print ".... Done \n"; exit 0; } }


It doesn't seem to work, no matter what the command is run, my question is: should I just use a pattern matching technique as opposed to assigning string value to $spooler?

Replies are listed 'Best First'.
Re: Backticks and Pattern Matching.
by halley (Prior) on Feb 05, 2008 at 15:30 UTC
    my $spooler = `lpstat -r`; if ( $spooler = "scheduler is not running" )
    You're assigning to $spooler in your conditional statement (not just the backtick statement). You really should be comparing, or pattern-matching.
    if ( $spooler eq "scheduler is not running" ) ... if ( index($spooler, "scheduler is not running") >= 0 ) ... if ( $spooler =~ /scheduler is not running/ ) ...
    Given that this is output from some other tool, I would guess that the last of those alternatives is the most useful.

    Update: After re-reading your question as posed, I guess I'll state what I thought was obvious-- I rarely want to assign (use the = operator) in a conditional statement. To compare numbers, you don't assign, you check for equality with the == operator. To compare strings, you don't assign, you check for equality with the eq operator. Also, the command output captured with backticks contains ALL of the standard output, newlines and all, so if your scheduler command really does JUST print "scheduler is not running", it STILL likely won't match exactly with the eq operator.

    --
    [ e d @ h a l l e y . c c ]

Re: Backticks and Pattern Matching.
by rgiskard (Hermit) on Feb 05, 2008 at 15:35 UTC
    When you use backticks and you don't chomp the results the returned value will include a newline.

    You must account for this in your strict comparison if you're not going to chomp (but please use eq, otherwise you're not comparing you are setting).
Re: Backticks and Pattern Matching.
by apl (Monsignor) on Feb 05, 2008 at 16:06 UTC
    If you had use strict;, you'd see that you were assigning (rather than tetsing) when you did if ( $spooler = "scheduler is not running" )

    In any event, you should use eq when comparing strings. == is for testing numbers, = is for assignment.

      my $spooler = `lpstat -r`; chomp($spooler); if ( $spooler eq "scheduler is not running" ) { system("lpsched"); }
      I've been using strict but the interpreter hadn't spit any messages out! I think you guys pretty much clarified my situation, thanks so much for the help you guys have provided!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-18 01:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found