[17326] in Perl-Users-Digest

home help back first fref pref prev next nref lref last post

Perl-Users Digest, Issue: 4748 Volume: 9

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Oct 28 03:05:21 2000

Date: Sat, 28 Oct 2000 00:05:08 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)
Message-Id: <972716707-v9-i4748@ruby.oce.orst.edu>
Content-Type: text

Perl-Users Digest           Sat, 28 Oct 2000     Volume: 9 Number: 4748

Today's topics:
    Re: *** Please help: Howto call a Perlscript within a J (Maggert)
    Re: [Newbie]: "if" statements (Andrew J. Perrin)
    Re: code counter v1 released :-) (Andrew J. Perrin)
        debugging cgi scripts ()
    Re: debugging cgi scripts (Prasanth A. Kumar)
    Re: Detecting socket closure <not.my.real.email@bellglobal.com>
    Re: func (a) func !a! func ~a~ func a .... more? <uri@sysarch.com>
    Re: How to protect perl program from pirates? (Maggert)
    Re: HTTPS request behaving differently (Martien Verbruggen)
    Re: Is there an active php group? (Maggert)
    Re: Perl CGI <godzilla@stomp.stomp.tokyo>
    Re: Perl on NT (Martien Verbruggen)
    Re: redirecting output changes order? innocent_smith@my-deja.com
    Re: redirecting output changes order? <lr@hpl.hp.com>
        Text area on a form with Perl CGI script <danmurph@worldnet.att.net>
    Re: Text area on a form with Perl CGI script <godzilla@stomp.stomp.tokyo>
    Re: This is a really NEAT perl switch (Martien Verbruggen)
    Re: what does /warn "$x" if "$x"/ mean <lincmad001@telecom-digest.zzn.com>
    Re: what does /warn "$x" if "$x"/ mean (Martien Verbruggen)
    Re: what does /warn "$x" if "$x"/ mean <lr@hpl.hp.com>
        Digest Administrivia (Last modified: 16 Sep 99) (Perl-Users-Digest Admin)

----------------------------------------------------------------------

Date: Sat, 28 Oct 2000 06:52:04 GMT
From: mag@ionet.net (Maggert)
Subject: Re: *** Please help: Howto call a Perlscript within a Javascript ? ***
Message-Id: <39fa72ed.191111293@news.ionet.net>

On Fri, 27 Oct 2000 22:30:23 +0100, Werner Hofer <whofer@dplanet.ch>
wrote:

>Hi
>i would like to call a Perlscript running on a web server within a
>Javasript.The Javasript
>is a part of an xy.shtml File. The Web Server is an Apache Server.
>
>The reason is following: I would like to measure the time for witch a
>user stays on a
>webpage. The onLoad function starts the timer and whenn the user will
>leave the webpage
>the onUnload function ( exactly the javascript function witch is
>associated with onUnload ! )
>stops the timer. And now a perl script should called within this
>javascript function with this
>calculated time as parameter !!!!  The perlscript then will write this
>time in a database. The perscript sends no return to the webpage. I
>tried with Server Site Include without success !
>What is wrong ? Or is this impossible ?
>When i can not use this mechanism, is it then possible to use an Event
>Handler which
>intercept the event of leaving a webpage ? If yes , ho to do that ?
>
>The only thing witch i want is to measure the time, call a perscript and
>insert this time
>into a database (Mysql ).
>
>I tried with the following source code ( The Perlscript was never
>executed ! When i
>called the Perlscript directly in the URL of the Browser, then the
>Perlscript was executed
>and inserted a value into the database. This means the perlscript is ok,
>only the call within
>a javascript does not work !
>
>Please can aybody help ? Thanks a lot in advance .
>Werner
>
>
>I tried with the following, reduced Html / Javascript Code ( extension :
>.shtml )
>
><html>
><head>
><script language="Javascript">
>var ti=12;
>function measure_time () {
>document.write("<!--#exec cgi='cgi-bin/test.pl?time=ti' -->");
>}
></script>
></head>
>
><body onUnload="measure_time()">
><a href="index.htm"> go to another page </a>
></body>
></html>
>
>
	I'm sure this can be done by parsing your log files, but I
know some people don't have this luxury.
	Using the SSI tag you've found doesn't work. Use the script
below instead. How you measure the time probably needs to be done on
the server side though. What I would do is to use the $pid to create a
file onLoad with the refferer and start time added to it. Then
onUnload since the $pid is the same, open the file, write the contents
to your log file and append the end time to it. You could do a
calculation of the time spent also at this time. Then unlink the file.
If the person moves to another page the process starts again and
everything is in your log which you could write another script to do
the analysis on. 

<script language="Javascript">
function measure_time () {
location.href=('/cgi-bin/test.pl');
}

	Your perl program must return a No Response Content header so
the page won't actually re-direct.

perldoc CGI
or go to http://stein.cshl.org/WWW/software/CGI/ and look under
content headers. There is an example of a No Response header.


MP


------------------------------

Date: 27 Oct 2000 23:35:31 -0400
From: aperrin@demog.berkeley.edu (Andrew J. Perrin)
Subject: Re: [Newbie]: "if" statements
Message-Id: <ud7glwscc.fsf@demog.berkeley.edu>

=?iso-8859-1?Q?S=E9bastien?= Ferrandez <sferrandez@wineandco.com> writes:

> I have to make a perl script, a language I've never tried out before but
> as i assume this is quite close to PHP, I tried that one which doesn't
> work :
> 
> print "avant : $jour\n\n";
> 
> if ($jour=="lun") {
>  $jour_complet = "Lundi";
> } elsif ($jour=="mar") {
>  $jour_complet = "Mardi";
> } elsif ($jour=="mer") {
>  $jour_complet = "Mercredi";
> } elsif ($jour=="jeu") {
>  $jour_complet = "Jeudi";
> } elsif ($jour=="ven") {
>  $jour_complet = "Vendredi";
> } elsif ($jour=="sam") {
>  $jour_complet = "Samedi";
> } elsif ($jour=="dim") {
>  $jour_complet = "Dimanche";
> } else { $jour_complet= "erreur"; }
> 
> print "le jour : $jour_complet\n\n";
> 
> $jour is a string containing "mar", thus it should output "Mardi" but in
> fact it outputs "Lundi".
> It seems quite trivial yet I miss the point here. Maybe too used to PHP
> ;)
> I've been looking at various perl tutorial sites but I found that to be
> syntaxically (almost?) correct.
> Any hint ? Thanks !
> 

Syntax isn't your problem (exactly).  Check out the perl docs, using
either perldoc perlop on your computer or at perldoc.perl.com.

== tests for numerical equality; since you're looking for a string
comparison, and you're looking for an inexact match, that's two
reasons why == is the wrong operator.

Try =~ with simple regexes and a modifier; something like:

if ($jour =~ /^lun/i) {

enjoy.

-- 
----------------------------------------------------------------------
Andrew J Perrin - Ph.D. Candidate, UC Berkeley, Dept. of Sociology  
Chapel Hill, North Carolina, USA - http://demog.berkeley.edu/~aperrin
        aperrin@socrates.berkeley.edu - aperrin@igc.apc.org


------------------------------

Date: 27 Oct 2000 23:32:38 -0400
From: aperrin@demog.berkeley.edu (Andrew J. Perrin)
Subject: Re: code counter v1 released :-)
Message-Id: <ug0lhwsh5.fsf@demog.berkeley.edu>

tarael200@aol.com (Tarael200) writes:

> #!/usr/bin/perl

no -w

>   open FILEN, "<$ARGV[0]" || die "Can't open file: $!";
>   @filetext = <FILEN>;

Rather inefficient for any large number of lines - and why bother?

>   close FILEN;
> 
>   while (scalar @filetext) {

while (<FILEN>) {

>         $totallines++;
> 

next if /^\s*$/;

> 
>         if ($poptext =~ /^#/) {

if (/^\s*#/) {

>                 $comments++;
>                 next;
>         }
>                 $codelines++;
>   }
> 
> # Output the data:
> print "CodeCounter $version by iless (tarael200\@aol.com) \n";
> print " Counted $totallines lines, $comments comments, and $codelines lines of
> code.\n";
> }
>         

-- 
----------------------------------------------------------------------
Andrew J Perrin - Ph.D. Candidate, UC Berkeley, Dept. of Sociology  
Chapel Hill, North Carolina, USA - http://demog.berkeley.edu/~aperrin
        aperrin@socrates.berkeley.edu - aperrin@igc.apc.org


------------------------------

Date: 28 Oct 2000 05:27:21 GMT
From: kmead@socrates.Berkeley.EDU ()
Subject: debugging cgi scripts
Message-Id: <8tdo3p$qbj$1@agate.berkeley.edu>

I've just found out that I'm now in charge of a bunch of
Perl CGI scripts. While I'm quite familiar with perl scripts
I'm less so with perl CGI scripts.

My question is, what is the best way to go about debugging
CGI scripts? Is there a better solution than just putting
in print statements? I'd love to be able to single-step in
a debugger through some of these scripts.

Thanks,

Keith


------------------------------

Date: Sat, 28 Oct 2000 06:00:45 GMT
From: kumar1@home.com (Prasanth A. Kumar)
Subject: Re: debugging cgi scripts
Message-Id: <m3zojpo67m.fsf@C654771-a.frmt1.sfba.home.com>

kmead@socrates.Berkeley.EDU () writes:

> I've just found out that I'm now in charge of a bunch of
> Perl CGI scripts. While I'm quite familiar with perl scripts
> I'm less so with perl CGI scripts.
> 
> My question is, what is the best way to go about debugging
> CGI scripts? Is there a better solution than just putting
> in print statements? I'd love to be able to single-step in
> a debugger through some of these scripts.
> 
> Thanks,
> 
> Keith

Yes, you can run the CGI scripts through the perl debugger
manually. If you use the CGI module, it will ask you to type any
parameters to pass to the script and then you press ctl-d to continue
with the script and debugging.

-- 
Prasanth Kumar
kumar1@home.com


------------------------------

Date: Sat, 28 Oct 2000 05:51:06 GMT
From: "MNJP" <not.my.real.email@bellglobal.com>
Subject: Re: Detecting socket closure
Message-Id: <ePtK5.75100$oN2.3100601@news20.bellglobal.com>


That is not correct. If the socket is set to non-blocking mode (the way I
prefer it) using fcntl(), then read and sysread will return 0 bytes until
something is received.

0 means 0. Nothing read. It does not mean the socket got closed.

if ($socket && sysread($socket, $in, X)) { do so and so } is usually good
enough.

"Uri Guttman" <uri@sysarch.com> wrote in message
news:x7em12kmsy.fsf@home.sysarch.com...
> >>>>> "FH" == Fulko Hew <fulko@wecan.com> writes:
>
>   FH> I am fighting with a problem w.r.t. detecting when a remote end
>   FH> of a socket has closed on me.
>
>   FH>    unless (defined(send($socket, $string, 0))) {
>   FH>         print "write returning error\n";
>   FH>         return 1;
>   FH>    }
>
>   FH> I try doing a select for error conditons first. It returns no
errors.
>
> closed sockets are detected by reading 0 bytes. just do a normal
read/sysread
> and check that its size is 0.
>
> uri
>
> --
> Uri Guttman  ---------  uri@sysarch.com  ----------
http://www.sysarch.com
> SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX
Consulting
> The Perl Books Page  -----------
http://www.sysarch.com/cgi-bin/perl_books
> The Best Search Engine on the Net  ----------
http://www.northernlight.com




------------------------------

Date: Sat, 28 Oct 2000 04:57:39 GMT
From: Uri Guttman <uri@sysarch.com>
Subject: Re: func (a) func !a! func ~a~ func a .... more?
Message-Id: <x77l6tlfzv.fsf@home.sysarch.com>

>>>>> "DB" == Dave Brondsema <brondsem@my-deja.com> writes:

  DB> ok, I tried some things and looked through some perl documentation and
  DB> you're right.  What I really want to know is what can I use instead
  DB> of / in a regexp.

  DB> print qq(1);
  DB> print qq~2~;
  DB> print qq!3!;
  DB> print qq/4/;

those are double quoted strings and not regexes. which book or education
source is leading you to these false beliefs?

uri

-- 
Uri Guttman  ---------  uri@sysarch.com  ----------  http://www.sysarch.com
SYStems ARCHitecture, Software Engineering, Perl, Internet, UNIX Consulting
The Perl Books Page  -----------  http://www.sysarch.com/cgi-bin/perl_books
The Best Search Engine on the Net  ----------  http://www.northernlight.com


------------------------------

Date: Sat, 28 Oct 2000 06:56:37 GMT
From: mag@ionet.net (Maggert)
Subject: Re: How to protect perl program from pirates?
Message-Id: <39fa7814.192430440@news.ionet.net>

On Sat, 28 Oct 2000 01:00:23 +0400, "Paul Antonov"
<antonov@lib.bmstu.ru> wrote:

>I need to protect my perl program...
>How can I crypt... compile...or do something else to
>privent free reaching of source codes?
>
>--
>

	Perl2exe is available for a price. It's alright but you give
up the ability to run the program persistently. In order to compile
for several types of Unix you'll need to fork out $500 for it though.

perl2exe.com


MP


------------------------------

Date: Sat, 28 Oct 2000 15:09:40 +1100
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: HTTPS request behaving differently
Message-Id: <slrn8vkkc4.c8.mgjv@martien.heliotrope.home>

On Fri, 27 Oct 2000 21:32:38 GMT,
	kras_gadjokov@yahoo.com <kras_gadjokov@yahoo.com> wrote:
> I am running absolutely identical Perl code on my Windows NT and on UK
> Shells - they are running Red Hat Linux.
> 
> The version of Perl is also the same -  5.005_03 - on both computers.
> 
> Everything runs and behaves identically, NO error messages from Perl,
> but an HTTPS request using LWP::UserAgent returns just nothing (the
> content of the request is empty when run on UK Shellls).
> 
> Again - there is NO error messages. My code inculdes HTTP requests with
> the same class LWP::UserAgent - works just fine, no problem . Only the
> HTTPS request doesn't work.

Are you checking for errors? Or are you hoping that the modules will
warn or die themselves? They probably won't. I still suspect that on one
of the machines some form of SSL support is installed while on the
others it isn't. 

You say the contents of the request are empty, and there are no errors.
You do actually mean that you are checking for errors, and there are
none?

Just show us some code, then at least we can try to reproduce the
problem on the thoiusands of mahcines all around the world which are
owned by people reading this. Then maybe we can tell you whether it's a
bug in the modules, or a bug in your program.

> Is there any way to check if something specific is required on
> the Linux shell to get HTTPS working?

It has nothing to do with the shell. It has everything to do with the
installation of Perl. Did you check whether the prerequisites for SSL
with LWP are installed?

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | If at first you don't succeed, try
Commercial Dynamics Pty. Ltd.   | again. Then quit; there's no use
NSW, Australia                  | being a damn fool about it.


------------------------------

Date: Sat, 28 Oct 2000 06:58:16 GMT
From: mag@ionet.net (Maggert)
Subject: Re: Is there an active php group?
Message-Id: <39fa78ea.192644758@news.ionet.net>

On Fri, 27 Oct 2000 13:25:27 -0400, </michael> wrote:

>
No they were busted for selling PHP to kids! <grin>


MP


------------------------------

Date: Fri, 27 Oct 2000 22:38:00 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Perl CGI
Message-Id: <39FA6638.F24E0C8F@stomp.stomp.tokyo>

dVoon wrote:
 
> 1) Consider the following code:
 
>          #!/usr/bin/perl -w
>            :
>          my $text = param('t');
>          $text = ($text =~ /\w+/)? $text: ''; # default to empty string.
> 


Your code contains a fatal syntax error, line 2.

Godzilla!


------------------------------

Date: Sat, 28 Oct 2000 15:11:34 +1100
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: Perl on NT
Message-Id: <slrn8vkkfm.c8.mgjv@martien.heliotrope.home>

On 26 Oct 2000 00:29:10 GMT,
	Clay Irving <clay@panix.com> wrote:
> On Thu, 26 Oct 2000 00:03:18 GMT, Martien Verbruggen 
> <mgjv@tradingpost.com.au> wrote:
> 
> >> Is Perl in NT (4.0) integrated, or has it to be installed additionally
> >> ?
> >
> >Are you asking whether it comes as part of the OS? No. And if it did,
> >I'd be very afraid to use it.
> 
> Can you imagine Visual Perl++ Studio?

Yes, I can, and it makes me shudder, just like MS VC++ and IBM VisualAge
products all make me shudder violently, and make me want to jump out of
windows.

ActiveState is doing this sort of thing to Perl. Check their Web site.

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | You can't have everything, where
Commercial Dynamics Pty. Ltd.   | would you put it?
NSW, Australia                  | 


------------------------------

Date: Sat, 28 Oct 2000 06:24:08 GMT
From: innocent_smith@my-deja.com
Subject: Re: redirecting output changes order?
Message-Id: <8tdre6$3b9$1@nnrp1.deja.com>

Thank you.  I added the following line to my script and everything
works fine:
$oldfh = select(STDOUT); $| = 1; select($oldfh);

Thanks for the help,

Mark

In article <39FA358E.EE0C0387@rochester.rr.com>,
  Bob Walton <bwalton@rochester.rr.com> wrote:
> innocent_smith@my-deja.com wrote:
> >
> > I'm really confused by this.  Why does directing
> > the output to a file change the order?
> >
> > milprod:/tmp # cat systest.pl
> > #!/usr/bin/perl
> > print "line1\n";
> > system "echo line2";
> > milprod:/tmp # ./systest.pl
> > line1
> > line2
> > milprod:/tmp # ./systest.pl > x
> > milprod:/tmp # cat x
> > line2
> > line1
> > milprod:/tmp #
> >
> > I must be missing something fundamental here I
> > just don't know what it is.
> >
> > Mark
> ...
> You need to look up the $| variable, which turns file buffering off
for
> the selected filehandle.
>
>     perldoc perlvar
>     perldoc -f select
>
> Basically, your print statements are putting characters in an internal
> buffer, and actual I/O operations happen only every once in a while
> (like when 8K characters are accumulated).  When you switch from your
> Perl program to another program, the other program will run and write
> out the contents of its buffered I/O operations when it terminates --
> and meanwhile, Perl's output is still pending in the bugger.  Setting
$|
> nonzero while a given filehandle is selected will set that filehandle
so
> that every print statement causes an actual physical I/O operation,
and
> buffering is not used.  This is referred to as setting autoflush on.
> Note that having autoflush on is much less efficient, since physical
I/O
> has to occur much more often.
> --
> Bob Walton
>


Sent via Deja.com http://www.deja.com/
Before you buy.


------------------------------

Date: Fri, 27 Oct 2000 23:41:51 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: redirecting output changes order?
Message-Id: <MPG.1463c00140cf735498ae78@nntp.hpl.hp.com>

In article <8td65o$jvd$1@nnrp1.deja.com> on Sat, 28 Oct 2000 00:21:17 
GMT, innocent_smith@my-deja.com <innocent_smith@my-deja.com> says...
> I'm really confused by this.  Why does directing
> the output to a file change the order?
> 
> milprod:/tmp # cat systest.pl
> #!/usr/bin/perl
> print "line1\n";
> system "echo line2";
> milprod:/tmp # ./systest.pl
> line1
> line2
> milprod:/tmp # ./systest.pl > x
> milprod:/tmp # cat x
> line2
> line1
> milprod:/tmp #
> 
> I must be missing something fundamental here I
> just don't know what it is.

perlfaq5: "How do I flush/unbuffer an output filehandle? Why must I do 
this?"

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


------------------------------

Date: Sat, 28 Oct 2000 04:31:05 GMT
From: "Daniel Murphy" <danmurph@worldnet.att.net>
Subject: Text area on a form with Perl CGI script
Message-Id: <dEsK5.8830$UL.535735@bgtnsc07-news.ops.worldnet.att.net>

I'm having a problem with a Perl CGI script when writing
text area input to a file.

A web page has a form with a text area which is posted to
a text file. The text gets put into the file OK , but I have problems
with Netscape and IE when clicking on Submit.

Netscape - An error box says "Document contains no data."
Explorer - The end of the URL has the name of the CGI script and
                   the browser window is blank.

I want the original web page with the text area to remain in the
browser display when clicking on Submit. Or, can I have the browser
return to the previous page?

In the Perl CGI file -   print "Content-type: text/html\n\n";

In the HTML file -      <form action="perlscript.cgi" method="post">

Also, the wrap feature works OK in the text area window, but
the same wrapping "newlines" do not appear in the text file.
Will I need to add something to the CGI to handle that?

Thanks.






------------------------------

Date: Fri, 27 Oct 2000 22:28:47 -0700
From: "Godzilla!" <godzilla@stomp.stomp.tokyo>
Subject: Re: Text area on a form with Perl CGI script
Message-Id: <39FA640F.9ADAC791@stomp.stomp.tokyo>

Daniel Murphy wrote:

(snipped)
 
> I'm having a problem with a Perl CGI script 
> when writing text area input to a file.

> A web page has a form with a text area which
> is posted to a text file. The text gets put 
> into the file OK , but I have problems with 
> Netscape and IE when clicking on Submit.
 
> Netscape - An error box says "Document contains no data."
> Explorer - The end of the URL has the name of the CGI 
             script and the browser window is blank.


You need to provide return data after your form
button is activated. You may have your script
print a simple message such as,

"Data accepted" 

or

"Thank you."

You may simply print a space if you want a blank
page after form submission. Another technique is
to have your script perform a redirect to another
page by printing a location and only a location,

print "Location: http... etc..\n\n";

Those double \n are required.

You must return some data to a browser after
form submission to prevent this error message.
Incidently, Mozilla handles this perfectly.
How MSIE handles this, is a program error
within MSIE itself.


Godzilla!


------------------------------

Date: Sat, 28 Oct 2000 17:26:19 +1100
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: This is a really NEAT perl switch
Message-Id: <slrn8vkscb.c8.mgjv@martien.heliotrope.home>

On Sat, 28 Oct 2000 03:50:19 GMT,
	webqueen, queen of the web <webqueen@my-deja.com> wrote:
> Check out p495 in Programming Perl (3rd Ed), and the -i switch. Wow this
> is cool- you can modify a file without opening, reading, printing,
> closing.

A word of warning: You are NOT changing the file. You are creating a new
one. Watch:

# ls -il fee
 193439 -rw-rw-r--    1 mgjv     users       13151 Oct 23 20:29 fee
# perl -pi -e 's/foo/bar/' fee
# ls -il fee
 191764 -rw-rw-r--    1 mgjv     users       13151 Oct 28 17:17 fee

See? The inode changed. This is not the same file. That may be a
quibble, but it is important in some situations.

> Say you got a cgi script that you need to change a variable x1 to x2
> (the backup part SAVES the original in that file ID):
> 
>   perl -pi'*.backup' -e 's/x1/x2/' cgifile

# perl -pi.backup -e 's/x1/x2/' file

The one that now has the .backup extension has the inode of the old one.

> NEAT! Perl does it again :)

And, what's more, Perl did this in version 1.0, back in '87. And it
behaved in the same way. 

An extract from the perl 1.0 man page:

[snip]
       -i<extension>
            specifies  that  files  processed by the <> construct
            are to be edited in-place.  It does this by  renaming
            the  input  file, opening the output file by the same
            name, and selecting that output file as  the  default
            for print statements.  The extension, if supplied, is
            added to the name of the old file to  make  a  backup
            copy.
[snip]

This particular paragraph hasn't changed much since then :).  This
explains why the inode changes, and in which order things happen.

# perldoc perlrun

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | Make it idiot proof and someone will
Commercial Dynamics Pty. Ltd.   | make a better idiot.
NSW, Australia                  | 


------------------------------

Date: Fri, 27 Oct 2000 22:02:19 -0700
From: Linc Madison <lincmad001@telecom-digest.zzn.com>
Subject: Re: what does /warn "$x" if "$x"/ mean
Message-Id: <271020002202197878%lincmad001@telecom-digest.zzn.com>

In article <39F92942.540625EF@acm.org>, John W. Krahn <krahnj@acm.org>
wrote:

> [Please start a new thread instead of replying to a previous thread]
> 
> 
> joechakra@my-deja.com wrote:
> > 
> > Here is a short perl script(?) I wrote to split images and it works:
>                                  ^^^^^^^
> >     use Image::Magick;
> >     $fname=shift;
> >     print "input file=$fname\n";
> >     $image = new Image::Magick;
> >     $x=$image->Read($fname);
> >     warn "$x" if "$x";
> >     $count=0+$x;
> >     $of="out001";
> >     print "$count";
> > 
> > -------------------------------------------
> > My question what is the meaning of
> >     warn "$x" if "$x";
> >     $count=0+$x;
> 
> You wrote it. Why did you write it this way? What do you think it is
> supposed to do?

Also, if you want to know what value $x holds, why not just insert
print "x = [$x]\n";
before the "warn" statement?

You can also look inside the Image::Magick file (Magick.pm in the
directory Image within your Perl libraries directory) and see what the
Read method is supposed to return.

> > Is $x a string, a number, a list, or a class? warn does not produce any
>            yes       yes       no         no
> 
> It is a scalar variable that can hold a string or a number or a
> reference depending on context.
> perldoc perldata

warn "$x" if "$x";

means exactly the same thing as

if ($x) {
   warn $x;
}

If you don't know what "warn" does, look it up. Page 241 of the Camel
book, or search on the CPAN web site, or perldoc -f warn

$count = 0 + $x;

forces $x to be evaluated numerically and then assigns that numeric
value to $count. No disrespect intended, but that's in the first week
of any Perl programming course. It's on Day 3 of "Teach Yourself Perl 5
in 21 Days."

You might also try changing the top of the program to
#! perl -w
(with whatever path may need to be prepended to "perl")


------------------------------

Date: Sat, 28 Oct 2000 17:16:14 +1100
From: mgjv@tradingpost.com.au (Martien Verbruggen)
Subject: Re: what does /warn "$x" if "$x"/ mean
Message-Id: <slrn8vkrpe.c8.mgjv@martien.heliotrope.home>

On Fri, 27 Oct 2000 03:32:24 GMT,
	joechakra@my-deja.com <joechakra@my-deja.com> wrote:
> Here is a short perl script(?) I wrote to split images and it works:
>     use Image::Magick;
>     $fname=shift;
>     print "input file=$fname\n";
>     $image = new Image::Magick;
>     $x=$image->Read($fname);
>     warn "$x" if "$x";
>     $count=0+$x;
>     $of="out001";
>     print "$count";

I think this program is supposed to read something like

use strict;
use Image::Magick;
my $fname = shift;
print "Input file '$fname'\n";
my $im = Image::Magick->new();
my $rc = $im->Read($fname);
die "(Read:$fname) $rc" if $rc;

# And here I have no clue anymore what you are trying to achieve, see
# later

> My question what is the meaning of
>     warn "$x" if "$x";
>     $count=0+$x;
> 
> Is $x a string, a number, a list, or a class? warn does not produce any
> output. But count prints as "2"

You have already had some explanations about what all this generally
means. $x can contain any scalar value. However, there are many scalar
values, and how you use them, and what you do with them, depends on
context.

In the context of Image::Magick $x is the result code of the call to
$image->Read($fname). For Image::Magick there are two different kinds of
methods, ones that returns objects and ones that don't.  The ones that
return objects should be checked like this:

my $obj = $im->Clone();
if (! ref $obj)
{
	# You have an error, exit the program
	die $obj;
}

In other words: if the returned thing isn't an object, it's an error
message. So, the return value from these methods is either an
Image::Magick object,

The other class of methods for Image::Magick returns undef on success,
or an error string on failure. So you check them this way:

my $rc = $im->Read($fname);
if ($rc)
{
	# There's an error, exit
	die $rc;
}

A convenient shorthand for this is

die $rc if $rc;

or if you only want to warn, as in your code:

warn $rc if $rc;

One other thing is important to know aboiut these error messages: They
contain an error number.

my $errno = $rc =~ /(\d+)/;

Error numbers of 400 and above mean that something went seriously
wrong, and you should destroy the object, and no longer use it.
Everything below that is up to you to decide.

What I don't understand is the 

$count = 0 + $x;

In the context of your program, $x can be either undefined (the load of
your image failed), or it contains a string. I suspect that you copied
this code from somewhere, and that that line is an attempt to put $x in
a numerical context, to extract the error number, as I did above with a
regex. But it doesn't make sense in your code, and I wouldn't use it
that way.

Doing 0 + $x makes no sense whatsoever if the read was successful,
because you're adding 0 to a reference, only to end up with a totally
meaningless number. In this case obviously 2.

You said that you wrote it. Maybe you should come out clean and say that
you copied it, and have another look at the code you copied it from,
maybe this time with the thoughts above in mind.

Martien
-- 
Martien Verbruggen              | 
Interactive Media Division      | You can't have everything, where
Commercial Dynamics Pty. Ltd.   | would you put it?
NSW, Australia                  | 


------------------------------

Date: Fri, 27 Oct 2000 23:54:04 -0700
From: Larry Rosler <lr@hpl.hp.com>
Subject: Re: what does /warn "$x" if "$x"/ mean
Message-Id: <MPG.146417e984ce961e98ae79@nntp.hpl.hp.com>

In article <slrn8vkrpe.c8.mgjv@martien.heliotrope.home>, 
mgjv@tradingpost.com.au says...

 ...

> One other thing is important to know aboiut these error messages: They
> contain an error number.
> 
> my $errno = $rc =~ /(\d+)/;

But that snippet doesn't capture it.

  my ($errno) = $rc =~ /(\d+)/;

It seems surprisingly easy to take one's eye off the ball on that one.

-- 
(Just Another Larry) Rosler
Hewlett-Packard Laboratories
http://www.hpl.hp.com/personal/Larry_Rosler/
lr@hpl.hp.com


------------------------------

Date: 16 Sep 99 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 16 Sep 99)
Message-Id: <null>


Administrivia:

The Perl-Users Digest is a retransmission of the USENET newsgroup
comp.lang.perl.misc.  For subscription or unsubscription requests, send
the single line:

	subscribe perl-users
or:
	unsubscribe perl-users

to almanac@ruby.oce.orst.edu.  

| NOTE: The mail to news gateway, and thus the ability to submit articles
| through this service to the newsgroup, has been removed. I do not have
| time to individually vet each article to make sure that someone isn't
| abusing the service, and I no longer have any desire to waste my time
| dealing with the campus admins when some fool complains to them about an
| article that has come through the gateway instead of complaining
| to the source.

To submit articles to comp.lang.perl.announce, send your article to
clpa@perl.com.

To request back copies (available for a week or so), send your request
to almanac@ruby.oce.orst.edu with the command "send perl-users x.y",
where x is the volume number and y is the issue number.

For other requests pertaining to the digest, send mail to
perl-users-request@ruby.oce.orst.edu. Do not waste your time or mine
sending perl questions to the -request address, I don't have time to
answer them even if I did know the answer.


------------------------------
End of Perl-Users Digest V9 Issue 4748
**************************************


home help back first fref pref prev next nref lref last post