Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6754533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:17:31+00:00 2026-05-26T13:17:31+00:00

I’m trying to debug my program, and I want to send out notifications to

  • 0

I’m trying to debug my program, and I want to send out notifications to a watcher if there are any notifications to get.

I have a method Notify that returns a list of files that the watcher wants to follow. My debug routine is just there to help me debug my program.

This works. I see the second debug routine printing out the value:

foreach my $watcher ($watch->Watcher) {
    debug qq(Sending out notifcations for ) . $watcher->User, 2;
    my @foo = $watcher->Notify;
    if (@foo) {
        debug qq(Change list to notify on: ) . join (", " => $watcher->Notify), 3;
        $watch->SendEmail($watcher);
    }

}

However, this fails:

foreach my $watcher ($watch->Watcher) {
    debug qq(Sending out notifcations for ) . $watcher->User, 2;
    if ($watcher->Notify) {
        debug qq(Change list to notify on: ) . join (", " => $watcher->Notify), 3;
        $watch->SendEmail($watcher);
    }

}

The difference between the first and second: In the first, I return $watcher->Notify to an array @foo and test on @foo. In the second, I test on the return value of $watcher->Notify.

The Notify subroutine looks like this:

sub Notify {
    my $self   = shift;
    my $change = shift;

    $self->{CHANGE} = [] if not exists $self->{CHANGE};
    if (defined $change) {
        push @{$self->{CHANGE}}, $change;
    }
    return sort @{$self->{CHANGE}};
}

Wait a second…

Okay, as I was typing this question out, I realized that when I say if ($watcher->Notify), I am returning into a scalar context, and when I say @foo = $watcher->Notify, I’m returning into a list context. Further testing with this:

foreach my $watcher ($watch->Watcher) {
    debug qq(Sending out notifcations for ) . $watcher->User, 2;
    my $foo = $watcher->Notify;   #Now a SCALAR and not a LIST
    if ($foo) {
        debug qq(Change list to notify on: ) . join (", " => $watcher->Notify), 3;
        $watch->SendEmail($watcher);
    }

}

Showed that $foo was null. Changing my method to:

sub Notify {
    my $self   = shift;
    my $change = shift;

    $self->{CHANGE} = [] if not exists $self->{CHANGE};
    if (defined $change) {
        push @{$self->{CHANGE}}, $change;
    }
    if (wantarray) {
       return sort @{$self->{CHANGE}};
    }
    else {
       return scalar @{$self->{CHANGE}};
   }
}

Now works. (Method now checks if I want an array and if I don’t, it returns an explicit scalar).

The question is why.

I thought if I returned an array into a scalar context, Perl should either be automatically doing a scalar for me (and return the number of items in the array) or at least join all elements in the array with $" as a separator. (I assumed the first, but the latter would have also worked).

For example:

#! /usr/bin/env perl

use strict;
use warnings;
use feature qw(say switch);
use Data::Dumper;

my @foo = qw(this that the other);

my $bar = @foo;

say "Bar = $bar   \@foo = @foo";

prints out:

Bar = 4   @foo = this that the other

Where did I go wrong? (I mean, besides selling all of my Apple stock when the price went all the way up to $40/share).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:17:32+00:00Added an answer on May 26, 2026 at 1:17 pm

    You weren’t returning an array. You were returning the result of sort called in scalar context. According to the docs:

    In scalar context, the behaviour of sort() is undefined.

    Which means it gets to do anything it wants, including returning undef.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.