CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
strict problems

 



aravindprasad
Novice

Jun 22, 2009, 10:19 AM

Post #1 of 10 (1537 views)
strict problems Can't Post

Hi all,

I have a strange situation on my hands. I have to use a set of Perl subroutines (coded by someone else) in my scripts. Now these subroutines are all *^&%* weird copyrighted stuff. Meaning I can copy them into my scripts (or call them or whatever) but I can not edit a bloody alphabet in them. Now this guy who wrote these subroutines had no sense of what strict is. So I have to break my strict rules to include them. What I am thinking of doing is this:

Code
/usr/bin/perl -w 
use strict;
# my strict stuff here
# and here
# and here ...

my $myvar;
{
no strict; # strict goes KABOOM
$myvar = ext_sub_main($arg1, $arg2, $arg3);
# this subroutine call other subs which are also not strict coded
}

# back in the strict code

### subroutines
sub ext_sub_main {
# YUKK stuff here
ext_sub1(blah blah);
ext_sub2(more blah);
return something;
}
sub ext_sub1 {
# HELP HELP!!! I cant see this stuff anymore
}
sub ext_sub2 {
# I need anti-depressants NOW
}


But is there a more elegant way of doing this without sullying my code with a "no strict"??? Any help to soothe the nerves of my obsessively compulsive strict coding brain is very much appreciated.

Thanks in advance,
-AP


1arryb
User

Jun 22, 2009, 10:27 AM

Post #2 of 10 (1530 views)
Re: [aravindprasad] strict problems [In reply to] Can't Post

Hi aravindprasad,

Move all of your "unstrict" code into a separate module, doing whatever you have to do to get it to compile, then "use" it in your "strict" program. I've tested this approach and it works.

Larry


FishMonger
Veteran

Jun 22, 2009, 11:39 AM

Post #3 of 10 (1529 views)
Re: [aravindprasad] strict problems [In reply to] Can't Post

What are the error and/or warning messages that you are recieving?

I doubt that the problem is due to strict. It's more likely due to using the -w flag which is global instead of the warnings pragma which is lexical.


FishMonger
Veteran

Jun 22, 2009, 11:58 AM

Post #4 of 10 (1528 views)
Re: [aravindprasad] strict problems [In reply to] Can't Post

The best solution is probably to scrap the other persons code and write your own corrected version.


aravindprasad
Novice

Jun 23, 2009, 6:32 AM

Post #5 of 10 (1518 views)
Re: [FishMonger] strict problems [In reply to] Can't Post

Thanks Larry and FishMonger. I like FishMonger's idea of rewriting that stuff especially since its not a very complex idea. But I might end in some legal trouble (its an algorithm which is probably patented). So I will not go there.

I am sure its not the -w sign which is the problem. I get the usual error if you forget a "my" when you use a variable the first time, "Global symbol $blahblah requires explicit package name in /path/to/script line 1245" (or something very similar).

I like the idea of creating a module in which I can insert the dirty subroutines intact. Since I am not really rewriting any of "their" code, I guess compiling should not be a problem. I will try that.

But here is something else. This guy actually opens a file handle in the main module and prints to it in other modules that he calls from the main module. Now I know that you can transfer references to file handles between subroutines. But that is not being done here. So that code goes like this

Code
sub mainsub { 
open FH, ">filename.ext" || die "failed";
sub1();
close FH;
}
sub sub1 {
print FH "printing to FH in sub1 though it was opened in mainsub\n";
return 1;
}


I had no clue this could be done. But I tried running just these modules separately and this works. Is this really allowed??? Or is this still allowed unofficially to cater for legacy code? These subroutines were written in 2002. I will appreciate a clarification.

Thanks and reagrds,
-AP


aravindprasad
Novice

Jun 23, 2009, 6:59 AM

Post #6 of 10 (1516 views)
Re: [aravindprasad] strict problems [In reply to] Can't Post

Addendum to my previous reply

As I said before the code works without errors even if you do not pass a reference to the file handle (refer code above). But it does not actually print to the file. Instead, it prints to the screen. So the code I posted above would output like below

Code
prompt:/> ./script.pl 
printing to FH in sub1 though it was opened in mainsub
prompt:/>

to the screen instead of the file referred to by FH, filename.ext. The file itself will be created with a 0 bytes size (but this is expected since I legally opened and closed a file handle)

I would have expected Perl to throw a tantrum and stop or at the least complain before carrying on execution. But it does neither and mind that I have the -w switch in place. This is weird and should probably be a bug.

-AP


FishMonger
Veteran

Jun 23, 2009, 8:02 AM

Post #7 of 10 (1513 views)
Re: [aravindprasad] strict problems [In reply to] Can't Post

The code you tested is not the same code you posted.


Code
[root@rkb-2 ~]# ls filename.ext 
ls: filename.ext: No such file or directory
[root@rkb-2 ~]# cat test.pl
#!/usr/bin/perl

use strict;
use warnings;

mainsub();

sub mainsub {
open FH, ">filename.ext" || die "failed";
sub1();
close FH;
}
sub sub1 {
print FH "printing to FH in sub1 though it was opened in mainsub\n";
return 1;
}

[root@rkb-2 ~]# ./test.pl
[root@rkb-2 ~]# cat filename.ext
printing to FH in sub1 though it was opened in mainsub



aravindprasad
Novice

Jun 23, 2009, 10:01 AM

Post #8 of 10 (1507 views)
Re: [FishMonger] strict problems [In reply to] Can't Post

Thanks for thr reply. Nope! I just wrote a representative example. I don't think I can post the code I am using directly because it is *&^$&%&* copyrighted (as mentioned above) which is where the troubles in my life started in the first instance :-). Anyways this is a complete code example:

Code
#!/usr/bin/perl -w 
use strict;

mainsub();

sub mainsub {
open FH, ">filename.ext" || die "failed";
sub1();
close FH;
}
sub sub1 {
print FH "printing to FH in sub1 though it was opened in mainsub\n";
return 1;
}

This will create a 0 bytes filename.ext and print the message to the screen. It is my personal opinion that this should at least elicit a warning from perl.

-AP


aravindprasad
Novice

Jun 23, 2009, 10:06 AM

Post #9 of 10 (1507 views)
Re: [1arryb] strict problems [In reply to] Can't Post

Hi Larry,


Quote
Move all of your "unstrict" code into a separate module, doing whatever you have to do to get it to compile, then "use" it in your "strict" program. I've tested this approach and it works.


In the end I did what you suggested. At least that saves me from seeing that code :-) Still have a knot in my tummy but I can live with that. You should have looked at the original code. It was terrible - syntax, readability, formatting, et al. I hope the guy who wrote it stopped coding in perl.

Thanks for the help guys.
Regards,
-AP


FishMonger
Veteran

Jun 23, 2009, 10:34 AM

Post #10 of 10 (1503 views)
Re: [aravindprasad] strict problems [In reply to] Can't Post


In Reply To
Anyways this is a complete code example:

Code
#!/usr/bin/perl -w 
use strict;

mainsub();

sub mainsub {
open FH, ">filename.ext" || die "failed";
sub1();
close FH;
}
sub sub1 {
print FH "printing to FH in sub1 though it was opened in mainsub\n";
return 1;
}

This will create a 0 bytes filename.ext and print the message to the screen. It is my personal opinion that this should at least elicit a warning from perl.

-AP


If that's true, then your perl installation is broken.

Other than using the -w switch, which you should not do, instead of the warnings pragma, that is the same as what I have already shown to work.

Please list, execute and post your example as I did.

Now, if you're saying that the actual production code, which obviously differs from the posted example, exhibits this behavior, then that is certainly possible. I can only help you troubleshoot the code you post.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives