[25981] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 8200 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Sat Jun 25 00:05:28 2005

Date: Fri, 24 Jun 2005 21:05:04 -0700 (PDT)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Fri, 24 Jun 2005     Volume: 10 Number: 8200

Today's topics:
         Re: ImageMagick <ron@savage.net.au>
        Example for User-defined subroutines for GetOptions ple <sun_tong@users.sourceforge.net>
    Re: Example for User-defined subroutines for GetOptions <skuo@psi.nsc.com>
    Re: Package fails because it refers to variable in main <tadmc@augustmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Sat, 25 Jun 2005 11:32:37 +1000
From:     Ron Savage <ron@savage.net.au>
Subject:  Re: ImageMagick
Message-Id: <2005625113237.728165@ron>

On Sat, 25 Jun 2005 01:04:51 +1000, lexjaquet@[no spam]msn.com wrote:

Hi

http://www.imagemagick.org/script/binary-releases.php




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

Date: Fri, 24 Jun 2005 20:51:46 -0400
From: * Tong * <sun_tong@users.sourceforge.net>
Subject: Example for User-defined subroutines for GetOptions please
Message-Id: <1119660707.9527d559bae65623964386c2d8d26e5f@teranews>

Hi, 

I need an example for the User-defined subroutines for GetOptions please.


,-----
| When GetOptions() encounters the option, it will call the subroutine
| with two or three arguments. The first argument is the name of the
| option. For a scalar or array destination, the second argument is the
| value to be stored. For a hash destination, the second arguments is the
| key to the hash, and the third argument the value to be stored. It is up
| to the subroutine to store the value, or do whatever it thinks is
| appropriate.
`-----

Since the destination is a subroutines, how can Perl tell if it is  scalar
or array or hash?

Anyway, I try to follow it, and here is my test program:

my @opt_xsel, @opt_xsel_type;

GetOptions(
	   'copy-of=s'  => \&xquerystr,
	   'value-of=s' => \&xquerystr,
	   );

sub xquerystr {
    my ($opt_name, $opt_var);
    warn "] $opt_name = $opt_var\n";
    push @opt_xsel, $opt_var;
    push @opt_xsel_type, $opt_name eq 'copy-of';
}

I pass the following parameters,

-c 1 -va 2 -c 3 -c 4 -va 5

but all $opt_name, $opt_var in xquerystr are empty.

How should I fix it?

Thanks

-- 
Tong (remove underscore(s) to reply)
  *niX Power Tools Project: http://xpt.sourceforge.net/
  - All free contribution & collection


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

Date: Fri, 24 Jun 2005 18:14:57 -0700
From: Steven Kuo <skuo@psi.nsc.com>
Subject: Re: Example for User-defined subroutines for GetOptions please
Message-Id: <Pine.LNX.4.60.0506241809420.13806@psi.nsc.com>

On Fri, 24 Jun 2005, * Tong * wrote:

> Hi,
>
> I need an example for the User-defined subroutines for GetOptions please.


It would be nice if you specified that you were interested that
particular subroutine in the Getopt::Long module.
                              ^^^^^^^^^^^


[snipped ...]

> Anyway, I try to follow it, and here is my test program:
>
> my @opt_xsel, @opt_xsel_type;
>
> GetOptions(
> 	   'copy-of=s'  => \&xquerystr,
> 	   'value-of=s' => \&xquerystr,
> 	   );
>
> sub xquerystr {
>    my ($opt_name, $opt_var);
>    warn "] $opt_name = $opt_var\n";
>    push @opt_xsel, $opt_var;
>    push @opt_xsel_type, $opt_name eq 'copy-of';
> }
>
> I pass the following parameters,
>
> -c 1 -va 2 -c 3 -c 4 -va 5
>
> but all $opt_name, $opt_var in xquerystr are empty.
>
> How should I fix it?




By assigning to $opt_name and $opt_var?
The first statement in your subroutine should be:

     my ($opt_name, $opt_var) = @_;
                               ^^^^^

-- 
Hope this helps,
Steven


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

Date: Fri, 24 Jun 2005 17:09:55 -0500
From: Tad McClellan <tadmc@augustmail.com>
Subject: Re: Package fails because it refers to variable in main program - how to fix?
Message-Id: <slrndbp15j.9op.tadmc@magna.augustmail.com>

Henry Law <lawshouse.public@btconnect.com> wrote:
> On 24 Jun 2005 17:44:47 GMT, anno4000@lublin.zrz.tu-berlin.de (Anno
> Siegel) wrote:


[ snip using a bunch of global "configuration" variables ]


> Thanks, Paul and Anno.  There is a reason why I'm trying to do what
> I'm doing but the faint whiff of not-right-ness has now become a foul
> reek so I fear I can't avoid some re-structuring.  
> 
> But before I crawl back to my editor, advise me once more: the
> routines I'm writing make quite intensive use of a small set of
> system-wide variables - the host name of the server, for example, the
> name of the database to connect to, the name of the log file, etc etc.
> I'm as against global variables as the next guy (learnt that in APL
> programming way back last century) but what is the most convenient way
> of making them accessible when they're needed?


Package (global) variables live in the Symbol Table, which is just
a hash with some special access methods.

Simply use your own hash instead of the symbol table hash:

   $cf{server} = '127.0.0.1';     # instead of $server
   $cf{db_name} = 'myDatabase;    # instead of $db_name
   ...

ie. have 1 global hash with 27 keys rather than 27 separate globals.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas


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

Date: 6 Apr 2001 21:33:47 GMT (Last modified)
From: Perl-Users-Request@ruby.oce.orst.edu (Perl-Users-Digest Admin) 
Subject: Digest Administrivia (Last modified: 6 Apr 01)
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: due to the current flood of worm email banging on ruby, the smtp
server on ruby has been shut off until further notice. 

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 V10 Issue 8200
***************************************


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