[22144] in Perl-Users-Digest

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

Perl-Users Digest, Issue: 4365 Volume: 10

daemon@ATHENA.MIT.EDU (Perl-Users Digest)
Wed Jan 8 09:11:39 2003

Date: Wed, 8 Jan 2003 06:10:09 -0800 (PST)
From: Perl-Users Digest <Perl-Users-Request@ruby.OCE.ORST.EDU>
To: Perl-Users@ruby.OCE.ORST.EDU (Perl-Users Digest)

Perl-Users Digest           Wed, 8 Jan 2003     Volume: 10 Number: 4365

Today's topics:
    Re: using subroutines defined in other scripts <jvandervloet@hotmail.com>
    Re: using subroutines defined in other scripts <nobull@mail.com>
    Re: using subroutines defined in other scripts <jvandervloet@hotmail.com>
    Re: using subroutines defined in other scripts <nobull@mail.com>
    Re: using subroutines defined in other scripts <jvandervloet@hotmail.com>
        Digest Administrivia (Last modified: 6 Apr 01) (Perl-Users-Digest Admin)

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

Date: Wed, 08 Jan 2003 11:31:33 GMT
From: "joeri" <jvandervloet@hotmail.com>
Subject: Re: using subroutines defined in other scripts
Message-Id: <p_TS9.391$ym5.66@afrodite.telenet-ops.be>

Hi,

thanks for the help. Here's what I did and it works. Any comments still
welcome...

PerlSubs.pm in folder c:/perl/lib/Subs contains:

package PerlSubs;
use Exporter;
@ISA = qw/Exporter/;
@EXPORT = qw/delete_duplicates max pushnew get_position get_first_position/;
@EXPORT_OK = qw/%seen $max @found @$array $array/;

sub pushnew (\@@) {
    my $array = shift;
    while (@_) {
        push @$array, $_[0] unless grep $_[0] eq $_, @$array;
        shift;
    }
}

1;

The new script has:

use Subs::PerlSubs;
use warnings;

package PerlSubs;
@seen = qw/I have/;
pushnew(@seen, "seen");
print "NEW SEEN: @seen\n";

And @seen now is: (I, have, seen).

Which is what was intended...

I also tried :

no PerlSubs;
print "MAIN: @seen\n";

And @seen still contains the right info...



Thanks again for the help guys.

"Sam Holden" <sholden@flexal.cs.usyd.edu.au> wrote in message
news:slrnb1lpb6.tp1.sholden@flexal.cs.usyd.edu.au...
> On Tue, 07 Jan 2003 14:20:35 GMT, joeri <jvandervloet@hotmail.com> wrote:
> > This is a multi-part message in MIME format.
> >
> > ------=_NextPart_000_005E_01C2B660.532E63E0
> > Content-Type: text/plain;
> > charset="iso-8859-1"
> > Content-Transfer-Encoding: quoted-printable
> >
> > 'use warnings' doesn't produce any kind of feedback.
> > 'use strict' gives all sorts of trouble... Too much to print here and in
=
> > fact none
> > of the info actually refers to &pushnew.
>
> use strict gives a message for me, using just the code you posted
> (in two files).
>
> Maybe you should fix all the problems use strict is pointing out
> while you're at it anyway...
>
> --
> Sam Holden
>




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

Date: 08 Jan 2003 12:25:24 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: using subroutines defined in other scripts
Message-Id: <u9znqbq157.fsf@wcl-l.bham.ac.uk>

"joeri" <jvandervloet@hotmail.com> posts TOFU:

> thanks for the help. Here's what I did and it works. Any comments still
> welcome...

OK, first comment, posting TOFU is rude.  It will gerally be
considered an insult by the person to whom you are reponding.  If you
really mean to thank someone then you should not insult them in the
same breath.

> PerlSubs.pm in folder c:/perl/lib/Subs contains:
> 
> package PerlSubs;

In Perl modules, unless you really understand what you are doing the
package name should always match the filename.  It should be Subs::PerlSubs.

> use Exporter;

The semantics of 'use Exporter' are undefined.  One day this may
change.  You should 'require Exporter'.

> @ISA = qw/Exporter/;
> @EXPORT = qw/delete_duplicates max pushnew get_position get_first_position/;
> @EXPORT_OK = qw/%seen $max @found @$array $array/;

You should enable wanrnings and strictures in all source files.  Like
ropes when climbing they may slow you down a little, particularly at
first but you'll more than recoup the investment in time the first
time you slip and fall.

> sub pushnew (\@@) {
>     my $array = shift;
>     while (@_) {
>         push @$array, $_[0] unless grep $_[0] eq $_, @$array;
>         shift;
>     }
> }

Arrays in Perl are not the correct datatype to represent sets.  Hashes
are.

> The new script has:
> 
> use Subs::PerlSubs;
> use warnings;
> 
> package PerlSubs;

Do not have your main script switch namespace unless you understand
what you are doing.

> @seen = qw/I have/;
> pushnew(@seen, "seen");
> print "NEW SEEN: @seen\n";
> 
> And @seen now is: (I, have, seen).
> 
> Which is what was intended...

But not for the right reason.  

Your file Subs/PerlSubs.pm does not define a package called
Subs::PerlSubs, it defines one called PerlSubs.  So when you 'use
Subs::PerlSubs' you define a subroutine PerlSubs::pushnew() and make
Exporter a base class for the class PerlSubs.  Then use tries to call
Subs::PerlSubs->import() - which, of course, does not exist.  ('use'
does not throw and error if the import method does not exist).

So &PerlSubs::pushnew never gets exported into the main:: namespace.

However you still manage to find it by changing the default namespace
from main:: to PerlSubs::.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Wed, 08 Jan 2003 12:57:11 GMT
From: "joeri" <jvandervloet@hotmail.com>
Subject: Re: using subroutines defined in other scripts
Message-Id: <HeVS9.535$ym5.76@afrodite.telenet-ops.be>

> OK, first comment, posting TOFU is rude.  It will gerally be
> considered an insult by the person to whom you are reponding.  If you
> really mean to thank someone then you should not insult them in the
> same breath.

I'm not sure what you mean by TOFU.
I wasn't aware of the fact that I did sth that is considered rude. All
replies to my first post were helpful
in finding the solution I posted last. If I offended anyone, I'm sorry.

If I understand correctly, here's what the .pm and the .pl should contain...

c:/perl/lib/Subs/PerlSubs.pm :

package Subs::PerlSubs;
require Exporter;
use strict;
use warnings;
@ISA = qw/Exporter/;
@EXPORT = qw/delete_duplicates max pushnew get_position get_first_position/;
@EXPORT_OK = qw/%seen $max @found @$array $array/;

sub pushnew (\@@) {
    my $array = shift;
    while (@_) {
        push @$array, $_[0] unless grep $_[0] eq $_, @$array;
        shift;
    }
}

1;

The new script (*.pl) now contains:

use Subs::PerlSubs;
use strict;
use warnings;

@seen = qw/I have/;
pushnew(@seen, "seen");
print "NEW SEEN: @seen\n";
print "MAIN: @seen\n";

Which gives for @seen: (I, have, seen);

Is this how it is supposed to be done?

J.

"Brian McCauley" <nobull@mail.com> wrote in message
news:u9znqbq157.fsf@wcl-l.bham.ac.uk...
> "joeri" <jvandervloet@hotmail.com> posts TOFU:
>
> > thanks for the help. Here's what I did and it works. Any comments still
> > welcome...
>
> OK, first comment, posting TOFU is rude.  It will gerally be
> considered an insult by the person to whom you are reponding.  If you
> really mean to thank someone then you should not insult them in the
> same breath.
>
> > PerlSubs.pm in folder c:/perl/lib/Subs contains:
> >
> > package PerlSubs;
>
> In Perl modules, unless you really understand what you are doing the
> package name should always match the filename.  It should be
Subs::PerlSubs.
>
> > use Exporter;
>
> The semantics of 'use Exporter' are undefined.  One day this may
> change.  You should 'require Exporter'.
>
> > @ISA = qw/Exporter/;
> > @EXPORT = qw/delete_duplicates max pushnew get_position
get_first_position/;
> > @EXPORT_OK = qw/%seen $max @found @$array $array/;
>
> You should enable wanrnings and strictures in all source files.  Like
> ropes when climbing they may slow you down a little, particularly at
> first but you'll more than recoup the investment in time the first
> time you slip and fall.
>
> > sub pushnew (\@@) {
> >     my $array = shift;
> >     while (@_) {
> >         push @$array, $_[0] unless grep $_[0] eq $_, @$array;
> >         shift;
> >     }
> > }
>
> Arrays in Perl are not the correct datatype to represent sets.  Hashes
> are.
>
> > The new script has:
> >
> > use Subs::PerlSubs;
> > use warnings;
> >
> > package PerlSubs;
>
> Do not have your main script switch namespace unless you understand
> what you are doing.
>
> > @seen = qw/I have/;
> > pushnew(@seen, "seen");
> > print "NEW SEEN: @seen\n";
> >
> > And @seen now is: (I, have, seen).
> >
> > Which is what was intended...
>
> But not for the right reason.
>
> Your file Subs/PerlSubs.pm does not define a package called
> Subs::PerlSubs, it defines one called PerlSubs.  So when you 'use
> Subs::PerlSubs' you define a subroutine PerlSubs::pushnew() and make
> Exporter a base class for the class PerlSubs.  Then use tries to call
> Subs::PerlSubs->import() - which, of course, does not exist.  ('use'
> does not throw and error if the import method does not exist).
>
> So &PerlSubs::pushnew never gets exported into the main:: namespace.
>
> However you still manage to find it by changing the default namespace
> from main:: to PerlSubs::.
>
> --
>      \\   ( )
>   .  _\\__[oo
>  .__/  \\ /\@
>  .  l___\\
>   # ll  l\\
>  ###LL  LL\\




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

Date: 08 Jan 2003 13:08:42 +0000
From: Brian McCauley <nobull@mail.com>
Subject: Re: using subroutines defined in other scripts
Message-Id: <u9r8bnpy4j.fsf@wcl-l.bham.ac.uk>

"joeri" <jvandervloet@hotmail.com> writes:

> > OK, first comment, posting TOFU is rude.  It will gerally be
> > considered an insult by the person to whom you are reponding.  If you
> > really mean to thank someone then you should not insult them in the
> > same breath.
> 
> I'm not sure what you mean by TOFU.

Text On-top.  Full-quote under.  i.e. putting the entirity of the
message to which you are replying at the end of your message.

> I wasn't aware of the fact that I did sth that is considered rude.

In general, in cyberspace just as in meatspace, you should, on
entering a new cultural context, before taking an active role, make
some effort to observe the interactions between the people already
there. Make a note of what behaviour is consdered acceptable and what
behaviour is generally met with criticism or agression.

You should then seek to avoid the behaviours that you have observed to
be met with agression.

It is odd that many people who would instinctively realise the need
for this "lurking" in meatspace seem to have geat difficulty realising
the need for it in cyberspace.

It should be pointed out that failure to lurk is, in itself, almost
universally considered rude in all cultures.

Alternatively when you go into an unfamiliar cultural evironment you
can pick up a guide book to the ettiquette that applies there.  There
are numerous Usenet ettiquette guides to be found on the 'net.

However, as such guides point out, there are some cultural variations
between newsgroups so lurking and/or looking out for a
newsgroup-specific guide is still a good idea.  In the case of
comp.lang.perl.* I'd recommend Tad's regularly posted posting
guidelines.

> If I understand correctly, here's what the .pm and the .pl should contain...
> 
> c:/perl/lib/Subs/PerlSubs.pm :
> 
> package Subs::PerlSubs;
> require Exporter;
> use strict;
> use warnings;
> @ISA = qw/Exporter/;
> @EXPORT = qw/delete_duplicates max pushnew get_position get_first_position/;

That won't compile.  One of the things the guidelines point out is
that when you post code you should always test the actual code you
intend to post before you post it.

With 'use strict' in effect the first mention of any variable needs to
be qualified with 'my' or 'our'.  In the case of @ISA an @EXPORT these
need to be package variables so should be qualified with 'our'.
(Note: this is a simplification of the full situation but will serve
you well for now).

> The new script (*.pl) now contains:
> 
> use Subs::PerlSubs;
> use strict;
> use warnings;
> 
> @seen = qw/I have/;
> pushnew(@seen, "seen");
> print "MAIN: @seen\n";
> 
> Which gives for @seen: (I, have, seen);

No it does't. It gives a compilation error: Global symbol "@seen"
requires explicit package name at ...
 
> Is this how it is supposed to be done?

Appart from the missing 'my' and 'our' I can see nothing wrong.

-- 
     \\   ( )
  .  _\\__[oo
 .__/  \\ /\@
 .  l___\\
  # ll  l\\
 ###LL  LL\\


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

Date: Wed, 08 Jan 2003 13:57:46 GMT
From: "joeri" <jvandervloet@hotmail.com>
Subject: Re: using subroutines defined in other scripts
Message-Id: <u7WS9.684$ym5.108@afrodite.telenet-ops.be>

> In general, in cyberspace just as in meatspace, you should, on
> entering a new cultural context, before taking an active role, make
> some effort to observe the interactions between the people already
> there. Make a note of what behaviour is consdered acceptable and what
> behaviour is generally met with criticism or agression.
>
> You should then seek to avoid the behaviours that you have observed to
> be met with agression.
>
> It is odd that many people who would instinctively realise the need
> for this "lurking" in meatspace seem to have geat difficulty realising
> the need for it in cyberspace.
>
> It should be pointed out that failure to lurk is, in itself, almost
> universally considered rude in all cultures.
>
> Alternatively when you go into an unfamiliar cultural evironment you
> can pick up a guide book to the ettiquette that applies there.  There
> are numerous Usenet ettiquette guides to be found on the 'net.
>
> However, as such guides point out, there are some cultural variations
> between newsgroups so lurking and/or looking out for a
> newsgroup-specific guide is still a good idea.  In the case of
> comp.lang.perl.* I'd recommend Tad's regularly posted posting
> guidelines.

Maybe people such as yourself can point out to me what I'm doing wrong when

I do the thing that is considered 'not done' in a cultural context that is
new to me,

so that I can bear this in mind if and when I choose to take part in this
culture again.

> > package Subs::PerlSubs;
> > require Exporter;
> > use strict;
> > use warnings;
> > @ISA = qw/Exporter/;
> > @EXPORT = qw/delete_duplicates max pushnew get_position
get_first_position/;
>
> That won't compile.  One of the things the guidelines point out is
> that when you post code you should always test the actual code you
> intend to post before you post it.
>
> With 'use strict' in effect the first mention of any variable needs to
> be qualified with 'my' or 'our'.  In the case of @ISA an @EXPORT these
> need to be package variables so should be qualified with 'our'.
> (Note: this is a simplification of the full situation but will serve
> you well for now).

So what you say is that I should do this?

package Subs::PerlSubs;
require Exporter;
use strict;
use warnings;

our(@ISA, @EXPORT, @EXPORT_OK);

@ISA = qw/Exporter/;
@EXPORT = qw/delete_duplicates max pushnew get_position get_first_position/;
@EXPORT_OK = qw/%seen $max @found @$array $array/;

> That won't compile.  One of the things the guidelines point out is
> that when you post code you should always test the actual code you
> intend to post before you post it.

I did test the code before posting and it worked fine.

J.



"Brian McCauley" <nobull@mail.com> wrote in message
news:u9r8bnpy4j.fsf@wcl-l.bham.ac.uk...
> "joeri" <jvandervloet@hotmail.com> writes:
>
> > > OK, first comment, posting TOFU is rude.  It will gerally be
> > > considered an insult by the person to whom you are reponding.  If you
> > > really mean to thank someone then you should not insult them in the
> > > same breath.
> >
> > I'm not sure what you mean by TOFU.
>
> Text On-top.  Full-quote under.  i.e. putting the entirity of the
> message to which you are replying at the end of your message.
>
> > I wasn't aware of the fact that I did sth that is considered rude.
>
> In general, in cyberspace just as in meatspace, you should, on
> entering a new cultural context, before taking an active role, make
> some effort to observe the interactions between the people already
> there. Make a note of what behaviour is consdered acceptable and what
> behaviour is generally met with criticism or agression.
>
> You should then seek to avoid the behaviours that you have observed to
> be met with agression.
>
> It is odd that many people who would instinctively realise the need
> for this "lurking" in meatspace seem to have geat difficulty realising
> the need for it in cyberspace.
>
> It should be pointed out that failure to lurk is, in itself, almost
> universally considered rude in all cultures.
>
> Alternatively when you go into an unfamiliar cultural evironment you
> can pick up a guide book to the ettiquette that applies there.  There
> are numerous Usenet ettiquette guides to be found on the 'net.
>
> However, as such guides point out, there are some cultural variations
> between newsgroups so lurking and/or looking out for a
> newsgroup-specific guide is still a good idea.  In the case of
> comp.lang.perl.* I'd recommend Tad's regularly posted posting
> guidelines.
>
> > If I understand correctly, here's what the .pm and the .pl should
contain...
> >
> > c:/perl/lib/Subs/PerlSubs.pm :
> >
> > package Subs::PerlSubs;
> > require Exporter;
> > use strict;
> > use warnings;
> > @ISA = qw/Exporter/;
> > @EXPORT = qw/delete_duplicates max pushnew get_position
get_first_position/;
>
> That won't compile.  One of the things the guidelines point out is
> that when you post code you should always test the actual code you
> intend to post before you post it.
>
> With 'use strict' in effect the first mention of any variable needs to
> be qualified with 'my' or 'our'.  In the case of @ISA an @EXPORT these
> need to be package variables so should be qualified with 'our'.
> (Note: this is a simplification of the full situation but will serve
> you well for now).
>
> > The new script (*.pl) now contains:
> >
> > use Subs::PerlSubs;
> > use strict;
> > use warnings;
> >
> > @seen = qw/I have/;
> > pushnew(@seen, "seen");
> > print "MAIN: @seen\n";
> >
> > Which gives for @seen: (I, have, seen);
>
> No it does't. It gives a compilation error: Global symbol "@seen"
> requires explicit package name at ...
>
> > Is this how it is supposed to be done?
>
> Appart from the missing 'my' and 'our' I can see nothing wrong.
>
> --
>      \\   ( )
>   .  _\\__[oo
>  .__/  \\ /\@
>  .  l___\\
>   # ll  l\\
>  ###LL  LL\\




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

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.  

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 4365
***************************************


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