Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bareword & uselss variable

Status
Not open for further replies.

jrottman

Programmer
Jun 17, 2005
47
I am using posix to create a daemon of the perl app that I am working on. However, I have run into a small snag.

I have broken each of my sections into separate perl files and I have used require to import the separate perl files (not sure if this is the right way, but still learning here).

Any way, when testing my application, I get few errors that I am unsure of and need a little help figuring them out. Any help with this is greatly appreciated.

To start off with here are the errors that I receive.

Useless use of a variable in void context at ./main.pl line 8.
Name "daemon::daemonize" used only once: possible typo at ./main.pl line 8.
Name "datasource::emailHost" used only once: possible typo at ./main.pl line 11.
Unquoted string "setsid" may clash with future reserved word at daemon.pl line 13.
Bareword found in conditional at daemon.pl line 13.



And here is my code.

Main.pl -

#!/usr/bin/perl -w
require "daemon.pl";
require "datasource.pl";
#require "mail.pl";

package main; #Package Declaration

$daemon::daemonize;
while(1){
sleep(20);
print "$datasource::emailHost \n";
#$mail::chkMail;
}

daemon.pl-

#!/usr/bin/perl -w
# Purpose: Creates Perl Daemon for given datasource

use POSIX qw(setsid);
package daemon; #package delaration

sub daemonize{
defined(my $pid = fork)
or die "Can't fork: $!";
exit if $pid;
setsid
or die "Can't start a new session: $!";
}
1;




 
I'm going to make a couple suggestions, but they aren't likely to be all the changes that you need to make.

[ol]
[li]add use strict to all your perl files, as this will you help detect a lot of the basic syntax errors which you have.[/li]
[li]Instead of using require, create a module so that you can use use. Here is a demonstration of what I mean:[/li]
[/ol]

Code:
[gray][i]# Name:    Daemon.pm[/i][/gray]
[gray][i]# Purpose: Creates Perl Daemon for given datasource[/i][/gray]

[url=http://perldoc.perl.org/functions/package.html][black][b]package[/b][/black][/url] [green]Daemon[/green][red];[/red]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]base[/green] [red]qw([/red][purple]Exporter[/purple][red])[/red][red];[/red]
[url=http://perldoc.perl.org/functions/our.html][black][b]our[/b][/black][/url] [blue]@EXPORT[/blue] = [red]qw([/red][purple][/purple][red])[/red][red];[/red]
[black][b]our[/b][/black] [blue]@EXPORT_OK[/blue] = [red]qw([/red][purple]daemonize[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]POSIX[/green] [red]qw([/red][purple]setsid[/purple][red])[/red][red];[/red]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[url=http://perldoc.perl.org/functions/sub.html][black][b]sub[/b][/black][/url] [maroon]daemonize[/maroon] [red]{[/red]
	[url=http://perldoc.perl.org/functions/defined.html][black][b]defined[/b][/black][/url][red]([/red][url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$pid[/blue] = [url=http://perldoc.perl.org/functions/fork.html][black][b]fork[/b][/black][/url][red])[/red] or [url=http://perldoc.perl.org/functions/die.html][black][b]die[/b][/black][/url] [red]"[/red][purple]Can't fork: [blue]$![/blue][/purple][red]"[/red][red];[/red]
	[url=http://perldoc.perl.org/functions/exit.html][black][b]exit[/b][/black][/url] [olive][b]if[/b][/olive] [blue]$pid[/blue][red];[/red]
	[maroon]setsid[/maroon][red]([/red][red])[/red] or [black][b]die[/b][/black] [red]"[/red][purple]Can't start a new session: [blue]$![/blue][/purple][red]"[/red][red];[/red]
[red]}[/red]

[fuchsia]1[/fuchsia][red];[/red]

[teal]__END__[/teal]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]base - Establish IS-A relationship with base classes at compile time[/li]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
Core (perl 5.10.0) Modules used :
[ul]
[li]POSIX - Perl interface to IEEE Std 1003.1[/li]
[/ul]
[/tt]

Code:
[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]Daemon[/green] [red]qw([/red][purple]daemonize[/purple][red])[/red][red];[/red]

[gray][i]#require "datasource.pl";[/i][/gray]
[gray][i]#require "mail.pl";[/i][/gray]

[black][b]use[/b][/black] [green]strict[/green][red];[/red]

[maroon]daemonize[/maroon][red]([/red][red])[/red][red];[/red]

[olive][b]for[/b][/olive] [url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]$i[/blue] [red]([/red][fuchsia]1..20[/fuchsia][red])[/red][red]{[/red]
	[url=http://perldoc.perl.org/functions/sleep.html][black][b]sleep[/b][/black][/url][red]([/red][fuchsia]20[/fuchsia][red])[/red][red];[/red]
	[url=http://perldoc.perl.org/functions/print.html][black][b]print[/b][/black][/url] [red]"[/red][purple]Hello World[purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[/ul]
[/tt]

Now, this code is not necessarily meant to be functional. It's just meant to demonstrate how you should use packages to separate your code instead of using require, and then how you call these separate packages.

- Miller
 
Don't use the -w shebang line switch, use the warnings pragma instead:

use warnings;

it gives you better control over what warnings perl will generate.

You should also start now using the strict pragma:

use strict;

this will force you to write better perl code. Harder at first but much easier later.


What you are getting are warnings, not errors. They alert you to possible problems. "Used only once" is a common warning, especially when calling variables in another package from your main program. Perl only sees the one instance of the scalar variable in main:

$daemon::daemonize;
$datasource::emailHost

So it warns you about that potential problem.

$daemon::daemonize is used in a void context. It is not being used in any purposeful way:

$daemon::daemonize = 'foo';
$foo = $daemon::daemonize;

"Future reserved" words are unquoted lower-case stings, like setsid. Perl uses all lower-case names for it's own built-in functions, some which can be called without any arguments, like time. I don't know it setsid can be called that way or not but if you add '&' before or () after perl knows its a function call:

&setsid
setsid()

and will not bark a warning at you about a bareward.










------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
ahh, Miller replied while I was answering a phone call and writing a reply. Good stuff.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Ok I think I have the bulk of this figured out. I think I have learned alot more from the posts that everyone has made then I have learned from the all the books I have bought.

I do have one more question that I have yet to figure out. The whole purpose of this little application is a POC of a java application that I have written.

One piece of the puzzle is how do I connect to an email server and pull down all the emails. 99% of the emails will have an attachment that I want to keep. After that I want to delete all the emails on the server that were just pulled down.

I have found the perl module Net::pOP3 but this module doesn't seem to allow me to download the attachments. I know how to connect to the email server with this module, but other then that it doesn't do me much good.

Any help with this would be greatly appreciated.
 
I've never done it.. but it's written about here

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top